﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;

namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmPhanNhomNguoiDung : Telerik.WinControls.UI.RadForm
    {
        PhanNhomNguoiDung pnnd = new PhanNhomNguoiDung();
        public frmPhanNhomNguoiDung()
        {
            InitializeComponent();
            khoitaogridnguoidung();
            khoitaogridnhomnguoidung();
            loadluoinguoidung();
            loadluoinhomnguoidung();
            if (BienHeThong.ht_quyencapnhat == 1)
            {
                radButton1.Enabled = true;
            }
            else
            {
                radButton1.Enabled = false;
            }
            if (BienHeThong.ht_quyenxoa == 1)
            {
                btnResetUser.Enabled = true;
                radButton2.Enabled = true;
                radButton3.Enabled = true;
            }
            else
            {
                btnResetUser.Enabled = false;
                radButton2.Enabled = false;
                radButton3.Enabled = false;
            }
        }
        private void loadluoinhomnguoidungbyuserid(int userid)
        {
            this.radGridView2.DataSource = pnnd.loadgirdnhomnguoidungbyuserid(userid);
        }
        private void loadluoinhomnguoidung()
        {
            this.radGridView2.DataSource = pnnd.loadgirdnhomnguoidung();
        }
        private void khoitaogridnhomnguoidung()
        {
            this.radGridView2.EnableHotTracking = true;
            this.radGridView2.ShowFilteringRow = false;
            this.radGridView2.EnableFiltering = true;
            this.radGridView2.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView2.MasterTemplate.ShowFilteringRow = false;
            this.radGridView2.MasterTemplate.AutoExpandGroups = true;
            //this.radGridView1.TableElement.RowHeight = 65;
            this.radGridView2.MasterTemplate.AllowAddNewRow = false;
            this.radGridView2.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView2.MasterTemplate.AllowEditRow = true;
            this.radGridView2.MasterTemplate.AllowDeleteRow = false;
            this.radGridView2.MasterTemplate.EnableSorting = true;
            this.radGridView2.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView2.AutoScroll = true;
            this.radGridView2.EnableAlternatingRowColor = true;

            CustomCheckBoxColumn checkBoxColumn = new CustomCheckBoxColumn()
            {
                HeaderText = "",
                FieldName = "Chon",
                Name = "Chon",
                Width = 25,
                IsVisible = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView2.Columns.Add(checkBoxColumn);
            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn()
            {
                FieldName = "MaNhomUser",
                Name = "MaNhomUser",
                HeaderText = "Mã Nhóm",
                Width = 100,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView2.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn()
            {
                FieldName = "TenNhomUser",
                Name = "TenNhomUser",
                HeaderText = "Tên Nhóm",
                Width = 260,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView2.Columns.Add(textBoxColumn1);


            GridViewCommandColumn CommandColumn = new GridViewCommandColumn()
            {
                HeaderText = "Xem",
                Name = "Xem",
                Width = 40,
                IsVisible = true,
                TextAlignment = ContentAlignment.MiddleCenter,

                Image = Image.FromFile(Application.StartupPath + @"\\Images\\xem.png")
            };
            this.radGridView2.Columns.Add(CommandColumn);

        }
        private void loadluoinguoidungbymanhom(int manhom)
        {
            this.radGridView1.DataSource = pnnd.loadgirdnguoidungbymanhom(manhom);
        }
        private void loadluoinguoidung()
        {
            this.radGridView1.DataSource=pnnd.loadgirdnguoidung();
        }
        private void khoitaogridnguoidung()
        {
            this.radGridView1.EnableHotTracking = true;
            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.AutoExpandGroups = true;
            //this.radGridView1.TableElement.RowHeight = 65;
            this.radGridView1.MasterTemplate.AllowAddNewRow = false;
            this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView1.MasterTemplate.AllowEditRow = true;
            this.radGridView1.MasterTemplate.AllowDeleteRow = false;
            this.radGridView1.MasterTemplate.EnableSorting = true;
            this.radGridView1.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView1.AutoScroll = true;
            this.radGridView1.EnableAlternatingRowColor = true;

            CustomCheckBoxColumn checkBoxColumn = new CustomCheckBoxColumn()
            {
                HeaderText = "",
                FieldName = "Chon",
                Name = "Chon", 
                Width = 25, 
                IsVisible = true, 
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(checkBoxColumn);
            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn()
            {
                FieldName = "UserID",
                Name = "UserID",
                HeaderText = "ID",
                Width = 50,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn()
            {
                FieldName = "MaNV",
                Name = "MaNV",
                HeaderText = "Mã Nhân Viên",
                Width = 150,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn1);

            GridViewTextBoxColumn textBoxColumn2 = new GridViewTextBoxColumn()
            {
                FieldName = "TenNV",
                Name = "TenNV",
                HeaderText = "Tên Nhân Viên",
                Width = 200,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn2);

            GridViewTextBoxColumn textBoxColumn3 = new GridViewTextBoxColumn()
            {
                FieldName = "TenPB",
                Name = "TenPB",
                HeaderText = "Phòng Ban",
                Width = 250,
                IsVisible = true,
                ReadOnly = true,
                TextAlignment = ContentAlignment.MiddleCenter
            };
            this.radGridView1.Columns.Add(textBoxColumn3);


            GridViewCommandColumn CommandColumn = new GridViewCommandColumn()
            {
                HeaderText = "Xem",
                Name = "Xem",
                Width = 40,
                IsVisible = true,
                TextAlignment = ContentAlignment.MiddleCenter,

                Image = Image.FromFile(Application.StartupPath + @"\\Images\\xem.png")
            };
            this.radGridView1.Columns.Add(CommandColumn);
        }

        private void txtTimKiemUser_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiemUser, radGridView1, 1, 0);
        }

        private void txtTimKiemGroup_TextChanged(object sender, EventArgs e)
        {
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiemUser, radGridView2, 1, 0);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnResetUser_Click(object sender, EventArgs e)
        {
            DialogResult kq = RadMessageBox.Show("Bạn có muốn Reset các User : được chọn không ???", "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
            if (kq == DialogResult.Yes)
            {
                for (int i = 0; i < radGridView1.Rows.Count; i++)
                {
                    if (radGridView1.Rows[i].Cells[0].Value != null)
                    {
                        bool isChecked = (bool)radGridView1.Rows[i].Cells[0].Value;
                        if (isChecked == true)
                        {
                            pnnd.xoa(ClassXuLyChung.GetSafeInt(radGridView1.Rows[i].Cells[1].Value));

                        }
                    }
                }
                RadMessageBox.Show("Reset Thành Công", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                loadluoinguoidung();
                loadluoinhomnguoidung();
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < radGridView1.Rows.Count; i++)
            {
                if (radGridView1.Rows[i].Cells[0].Value != null)
                {
                    bool isChecked = (bool)radGridView1.Rows[i].Cells[0].Value;
                    if (isChecked == true)
                    {

                        for (int j = 0; j < radGridView2.Rows.Count; j++)
                        {
                            if (radGridView2.Rows[j].Cells[0].Value != null)
                            {
                                bool isCheckednh = (bool)radGridView2.Rows[j].Cells[0].Value;
                                if (isCheckednh == true)
                                {

                                    pnnd.them(ClassXuLyChung.GetSafeInt(radGridView1.Rows[i].Cells[1].Value), ClassXuLyChung.GetSafeInt(radGridView2.Rows[j].Cells[1].Value));

                                }
                            }
                        }

                    }
                }
            }
            RadMessageBox.Show("Phân Nhóm Thành Công", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
        }

        private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
        {
            try
            {

                if (radGridView1.CurrentRow.Cells[5].IsCurrent == true)
                    {

                        radGridView1.CurrentRow.Cells[0].Value = true;
                        loadluoinhomnguoidungbyuserid(ClassXuLyChung.GetSafeInt(radGridView1.CurrentRow.Cells[1].Value));




                    }
               
            }
            catch
            {
                return;
            }
        }

        private void radGridView2_CellClick(object sender, GridViewCellEventArgs e)
        {

            try
            {

                if (radGridView2.CurrentRow.Cells[3].IsCurrent == true)
                {

                    radGridView2.CurrentRow.Cells[0].Value = true;
                    loadluoinguoidungbymanhom(ClassXuLyChung.GetSafeInt(radGridView2.CurrentRow.Cells[1].Value));




                }

            }
            catch
            {
                return;
            }
        }

        private void radButton2_Click(object sender, EventArgs e)
        {
            DialogResult kq = RadMessageBox.Show("Bạn có muốn Reset các Group : được chọn không ???", "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
            if (kq == DialogResult.Yes)
            {
                for (int i = 0; i < radGridView2.Rows.Count; i++)
                {
                    if (radGridView2.Rows[i].Cells[0].Value != null)
                    {
                        bool isChecked = (bool)radGridView2.Rows[i].Cells[0].Value;
                        if (isChecked == true)
                        {

                            for (int j = 0; j< radGridView1.Rows.Count;j++)
                            {
                                if (radGridView1.Rows[j].Cells[0].Value != null)
                                {
                                    bool isCheckedid = (bool)radGridView1.Rows[j].Cells[0].Value;
                                    if (isCheckedid == true)
                                    {
                                        pnnd.xoanhom(ClassXuLyChung.GetSafeInt(radGridView1.Rows[j].Cells[1].Value), ClassXuLyChung.GetSafeInt(radGridView2.Rows[i].Cells[1].Value));

                                    }
                                }
                            }
                        }
                    }
                }
                RadMessageBox.Show("Reset Thành Công", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                loadluoinguoidung();
                loadluoinhomnguoidung();
            }
        }

        private void radButton3_Click(object sender, EventArgs e)
        {
            DialogResult kq = RadMessageBox.Show(String.Format("Bạn có muốn Reset User {0}\n trong nhóm {1} không ???", ClassXuLyChung.GetSafeString(radGridView1.CurrentRow.Cells[2].Value), ClassXuLyChung.GetSafeString(radGridView2.CurrentRow.Cells[2].Value)), "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
            if (kq == DialogResult.Yes)
            {
               
                         if(   pnnd.xoaonerow(ClassXuLyChung.GetSafeInt(radGridView1.CurrentRow.Cells[1].Value),ClassXuLyChung.GetSafeInt(radGridView2.CurrentRow.Cells[1].Value))==1)
                         {
                     RadMessageBox.Show("Reset Thành Công", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);             
                loadluoinguoidung();
                loadluoinhomnguoidung();
                         }
                         else
                         {
                              RadMessageBox.Show("Không tồn tại dữ liêu", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                         }

                
           
            }
        }
    }
}
